package com.lhj.bootdemo.proxy;

import com.lhj.bootdemo.proxy.service.MockDemoClient;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author lhj
 * @version 1.0
 * @date 2022/4/17 8:36 上午
 * @description: 动态代理的调用处理器
 */
@Slf4j
public class DemoClientInvocationHandler implements InvocationHandler {

    private MockDemoClient realClient;
    public DemoClientInvocationHandler(MockDemoClient realClient) {
        this.realClient = realClient;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        String name = method.getName();
        log.info("方法名字：{}"+name);
        if(name.equals("hello")){
            return realClient.hello();
        }

        if(name.equals("echo")){
            return method.invoke(realClient, args);
        }
        Object invoke = method.invoke(realClient, args);

        return invoke;
    }
}
